package com.google.code.afreechartapp.iris;


import org.afree.chart.AFreeChart;
import org.afree.chart.demo.DemoView;
import org.afree.chart.plot.XYPlot;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.code.afreechartapp.iris.R;

public class IRISDataViewer extends IRISBase {

    public static final String DOMAIN_AXIS_TYPE_ID = "domainAxisTypeId";
    public static final String RANGE_AXIS_TYPE_ID = "rangeAxisTypeId";
    public static final int REQUEST_CODE = 1;
    
    private XYPlot mPlot;
    private int mRangeAxisType = AxisType.PetalLength.ordinal();
    private int mDomainAxisType = AxisType.SepalLength.ordinal();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        AFreeChart chart = createChart(mRangeAxisType, mDomainAxisType);
        mPlot = chart.getXYPlot();
        
        DemoView view = new DemoView(this);
        view.setChart(chart);
       
        setContentView(view);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.iris_data_viewer_menu, menu);
      return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.menu_about:
                showAbout();
                break;
            case R.id.menu_change_axes:
                intent = new Intent(this, IRISConfig.class);
                intent.putExtra(DOMAIN_AXIS_TYPE_ID, mDomainAxisType);
                intent.putExtra(RANGE_AXIS_TYPE_ID, mRangeAxisType);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.menu_run_multi_viewer:
                intent = new Intent(this, IRISDataMultiViewer.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                throw new IllegalArgumentException("unknown menu id.");
        }
        return true;
    }
    
    @Override
    protected AFreeChart createChart(int domainType, int rangeType) {
        mDomainAxisType = domainType;
        mRangeAxisType = rangeType;
        return super.createChart(domainType, rangeType);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            mPlot.setDataset(
                    createDataset(
                            data.getIntExtra(DOMAIN_AXIS_TYPE_ID, -1),
                            data.getIntExtra(RANGE_AXIS_TYPE_ID, -1)));
        }
    }
}